﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

using System.Data;

using XMK.Tadong.Model;
using XMK.Framework.Model;
using XMK.Framework.DataAccess;
using XMK.Utils;

namespace XMK.Tadong.Data
{
    /// <summary>
    /// 基础数据结构化检索
    /// </summary>
    public class V_Base_Struct_KeywData
    {
        /// <summary>
        /// 数据库连接
        /// </summary>
        protected DbProvider dbProvider;

        public V_Base_Struct_KeywData(DbProvider dbProvider)
        {
            this.dbProvider = dbProvider;
        }

        private string RemoveWildChar(string keyw)
        {
            return string.IsNullOrWhiteSpace(keyw) ? string.Empty :
                keyw.Replace("\\", "").Replace("'", "").Replace("%", "");
        }

        public List<V_Base_Struct_KeywModel> QueryList(string keyw, PageInfo pageInfo, OrderInfo order)
        {
            DbCmdHelper cmd = new DbCmdHelper(dbProvider, "sp_page_querybasestruct", delegate
            {
                return DbCmdHelper.CreateParameters(dbProvider, true, "keyw").ToArray();
            });

            cmd.SetParamValues(order, pageInfo, RemoveWildChar(keyw));

            List<V_Base_Struct_KeywModel> structModelList = DbManager.GetPageInfos(cmd, pageInfo,
                delegate(out V_Base_Struct_KeywModel userInfo, DataRow row)
                {
                    userInfo = new V_Base_Struct_KeywModel
                    {
                        Table_Proc_Name = ParseHelper.Parse<string>(row["t_proc_name"]),
                        Table_EN_Name = ParseHelper.Parse<string>(row["t_eng_name"]),
                        Table_CN_Name = ParseHelper.Parse<string>(row["t_cn_name"]),
                        Keyw_Cnt = ParseHelper.Parse<int>(row["cnt"])
                    };
                });
            return structModelList;
        }

        public DataTable SearchList(string keyw)
        {
            string[] keywArray = keyw.Split(' ');
            for (int i = 0; i < keywArray.Length; i++)
            {
                keywArray[i] = RemoveWildChar(keywArray[i]);
            }
            string sqlStr = "select m.t_proc_name, m.t_eng_name, m.t_cn_name,m.t_cat,m.en_t_cat, n.cnt from tb_table_dict m join(select a.t_code, count(1) cnt from v_base_struct_keyw a where a.keyw like";
            for (int j = 0; j < keywArray.Length; j++)
            {
                if (j == 0)
                    sqlStr += "'%" + keywArray[j] + "%'";
                else
                    sqlStr += "and a.keyw like" + "'%" + keywArray[j] + "%'";
            }
            sqlStr += "group by a.t_code) n on m.t_code = n.t_code";

            DbCmdHelper cmd = new DbCmdHelper(dbProvider, sqlStr);

            cmd.DbCmdInfo.CommandType = CommandType.Text;

            return DbHelper.ExecFullDataTable(dbProvider, cmd.DbCmdInfo);
        }

        public List<BaseTableColumnDictInfo> QueryTableColumnList(string tabEngName)
        {
            DbCmdHelper cmd = new DbCmdHelper(dbProvider, 
                //"select dict_id, tab_eng_name, tab_cn_name, col_eng_name,col_cn_name, col_align, col_w, col_order, col_sort, crt_time, upd_time from base_table_col_dict where tab_eng_name = :p_tab_eng_name order by col_order asc", 
                "select  FIELD_CODE, FIELD_NAME, FIELD_SHOWWIDTH,COL_ALIGN,FIELD_SHOW,FIELD_ID from DICT_COL where TABLE_CODE = :p_tab_eng_name order by FIELD_ID asc", 
                delegate
                {
                    return DbCmdHelper.CreateParameters(dbProvider, "tab_eng_name").ToArray();
                });
            cmd.DbCmdInfo.CommandType = CommandType.Text;
            cmd.SetParamValues(tabEngName);
            return DbManager.GetInfoList(cmd, delegate(out BaseTableColumnDictInfo model, DataRow row)
            {
                model = new BaseTableColumnDictInfo
                {
                    //DictID = ParseHelper.Parse<long>(row["DICT_ID"]),
                    //Tab_Eng_Name = ParseHelper.Parse<string>(row["tab_eng_name"]),
                    //Tab_CN_Name = ParseHelper.Parse<string>(row["tab_cn_name"]),
                    //Col_Eng_Name = ParseHelper.Parse<string>(row["col_eng_name"]),
                    //Col_CN_Name = ParseHelper.Parse<string>(row["col_cn_name"]),
                    //Col_Align = ParseHelper.Parse<string>(row["col_align"]),
                    //Col_Width = ParseHelper.Parse<int>(row["col_w"]),
                    //Col_Order = ParseHelper.Parse<int>(row["col_order"]),
                    //Col_Sort = ParseHelper.Parse<int>(row["col_sort"]) == 1 ? true :false,
                    //CreateTime = ParseHelper.Parse<DateTime>(row["crt_time"]),
                    //UpdateTime = ParseHelper.Parse<DateTime>(row["upd_time"])
                    FIELD_CODE = ParseHelper.Parse<string>(row["FIELD_CODE"]),
                    FIELD_NAME = ParseHelper.Parse<string>(row["FIELD_NAME"]),
                    FIELD_SHOWWIDTH = ParseHelper.Parse<int>(row["FIELD_SHOWWIDTH"]),
                    COL_ALIGN = ParseHelper.Parse<string>(row["COL_ALIGN"]),
                    FIELD_SHOW = ParseHelper.Parse<string>(row["FIELD_SHOW"])
                   
                };
            });
        }

        public DataTable QueryTableData(string tabEngName, string keyw, PageInfo page, OrderInfo order)
        {
            keyw = keyw.Replace(" ", ",");
            DataTable table = null;
            DbCmdHelper cmd = new DbCmdHelper(dbProvider,
                tabEngName,
                delegate
                {
                    return DbCmdHelper.CreateParameters(dbProvider,true, "keyw").ToArray();
                });
            cmd.SetParamValues(order, page, keyw);
            table = DbHelper.ExecFullDataTable(dbProvider, cmd.DbCmdInfo);
            cmd.GetReturnPageInfo(page);
            return table;
        }
    }
}
